package com.jianzhi;

import java.text.ParseException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 网页的解析
 * 
 * @author Administrator
 *
 */
public class XmlParer {

	/**
	 * 根据网页的内容获得一个兼职信息的对象
	 * 
	 * @param content
	 * @return
	 */
	public static Info getInfo(String content) {
		Info info = null;

		info = new Info();

		Document doc = Jsoup.parse(content);

		// 获得网页的body
		Element body = doc.body();

		// 获得标题
		Elements elementsByTag = body.getElementsByTag("h1");

		// 将标题的内容放到info
		info.setTitle(elementsByTag.get(0).text());

		// 获得时间
		Elements elementsByTag2 = body.getElementsByTag("p");
		String rep = "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}";
		for (Element element : elementsByTag2) {
			String string = element.text();
			List<String> list = DownLoad.getList(element.text(), rep);
			if (list != null && list.size() != 0) {
				try {
					info.setDate(info.format.parse(list.get(0)));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}

		StringBuffer buffer = new StringBuffer();

		// 获得要求

		Elements elementsByTag3 = body.getElementsByTag("ul");

		for (Element element : elementsByTag3) {
			Elements elementsByTag4 = element.getElementsByTag("li");
			for (Element element2 : elementsByTag4) {
				String string = element2.html();
				string = string.replaceAll("兼职吧", "大学生兼职论坛");
				string = string.replaceAll("jianzhi8.com", "gojsp.org");
				string = string.replaceAll("src=\"/code/", "src=\"http://xa.jianzhi8.com/code/");
				buffer.append(string + "<br/>");
			}

			Elements elementsByTag5 = element.getElementsByTag("p");
			for (Element element2 : elementsByTag5) {
				String string = element2.text();
				string = string.replaceAll("兼职吧", "大学生兼职论坛");
				string = string.replaceAll("jianzhi8.com", "gojsp.org");
				buffer.append(string + "<br/>");
			}
		}

		info.setYaoqiu(buffer.toString());

		return info;
	}
}
